/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package roomarrange;

import com.sun.data.provider.impl.CachedRowSetDataProvider;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.DropDown;
import com.sun.webui.jsf.component.Listbox;
import com.sun.webui.jsf.model.list.ListItem;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.convert.IntegerConverter;
import javax.faces.event.ValueChangeEvent;
import javax.sql.DataSource;
import javax.sql.rowset.CachedRowSet;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version manChoice.java
 * @version Created on 2009-9-14, 13:38:57
 * @author ben
 */
public class manChoice extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
        v_usrDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.v_usrRowSet}"));
        v_usrDataProvider1.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.v_usrRowSet}"));
        v_attendeeDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.v_attendeeRowSet}"));
        t_grpsetDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.t_grpsetRowSet}"));
        v_usrDataProvider2.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.v_usrListA}"));
    }
    private CachedRowSetDataProvider v_usrDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getV_usrDataProvider() {
        return v_usrDataProvider;
    }

    public void setV_usrDataProvider(CachedRowSetDataProvider crsdp) {
        this.v_usrDataProvider = crsdp;
    }
    private IntegerConverter listbox1Converter = new IntegerConverter();

    public IntegerConverter getListbox1Converter() {
        return listbox1Converter;
    }

    public void setListbox1Converter(IntegerConverter ic) {
        this.listbox1Converter = ic;
    }
    private Listbox listbox1 = new Listbox();

    public Listbox getListbox1() {
        return listbox1;
    }

    public void setListbox1(Listbox l) {
        this.listbox1 = l;
    }
    private Listbox listbox2 = new Listbox();

    public Listbox getListbox2() {
        return listbox2;
    }

    public void setListbox2(Listbox l) {
        this.listbox2 = l;
    }
    private CachedRowSetDataProvider v_usrDataProvider1 = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getV_usrDataProvider1() {
        return v_usrDataProvider1;
    }

    public void setV_usrDataProvider1(CachedRowSetDataProvider crsdp) {
        this.v_usrDataProvider1 = crsdp;
    }
    private CachedRowSetDataProvider v_attendeeDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getV_attendeeDataProvider() {
        return v_attendeeDataProvider;
    }

    public void setV_attendeeDataProvider(CachedRowSetDataProvider crsdp) {
        this.v_attendeeDataProvider = crsdp;
    }
    private IntegerConverter listbox2Converter = new IntegerConverter();

    public IntegerConverter getListbox2Converter() {
        return listbox2Converter;
    }

    public void setListbox2Converter(IntegerConverter ic) {
        this.listbox2Converter = ic;
    }
    private CachedRowSetDataProvider t_grpsetDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getT_grpsetDataProvider() {
        return t_grpsetDataProvider;
    }

    public void setT_grpsetDataProvider(CachedRowSetDataProvider crsdp) {
        this.t_grpsetDataProvider = crsdp;
    }
    private IntegerConverter dropDown1Converter = new IntegerConverter();

    public IntegerConverter getDropDown1Converter() {
        return dropDown1Converter;
    }

    public void setDropDown1Converter(IntegerConverter ic) {
        this.dropDown1Converter = ic;
    }
    private DropDown dropDown1 = new DropDown();

    public DropDown getDropDown1() {
        return dropDown1;
    }

    public void setDropDown1(DropDown dd) {
        this.dropDown1 = dd;
    }
    private CachedRowSetDataProvider v_usrDataProvider2 = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getV_usrDataProvider2() {
        return v_usrDataProvider2;
    }

    public void setV_usrDataProvider2(CachedRowSetDataProvider crsdp) {
        this.v_usrDataProvider2 = crsdp;
    }
    private IntegerConverter integerConverter2 = new IntegerConverter();

    public IntegerConverter getIntegerConverter2() {
        return integerConverter2;
    }

    public void setIntegerConverter2(IntegerConverter ic) {
        this.integerConverter2 = ic;
    }

    // </editor-fold>
    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public manChoice() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("manChoice Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }

    // </editor-fold>
    // Perform application initialization that must complete
    // *after* managed components are initialized
    // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
        int grpid = 0;
        if (this.isPostBack()) {
        } else {

            try {

                
                String sapp = this.getSessionBean1().getSApp();
                if (sapp.equalsIgnoreCase("room")) {
                    grpid=1; //如果是room默认的为院领导
                }else{
                    grpid=33; //如果是网上派警默认的为备选法警
                }
                CachedRowSet crs = this.getSessionBean1().getV_usrListA();
                crs.setObject(1, grpid);
                crs.execute();
            } catch (SQLException ex) {
                Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex);
            }
            this.getDropDown1().setSelected(grpid);
        }

    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
        v_usrDataProvider.close();
        v_usrDataProvider1.close();
        v_attendeeDataProvider.close();
        t_grpsetDataProvider.close();
        v_usrDataProvider2.close();
    }

    public String button1_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        Object oid = null;
        oid = this.getListbox1().getSelected();
        if (oid == null) {
            info("请选择人员");
            return null;
        }
        int iusrid = 0;
        String sMeetingid = "";
        iusrid = (Integer) oid;
        sMeetingid = this.getSessionBean1().getSUUID();
        //生成临时数据
        ApplicationContext ctx = new ClassPathXmlApplicationContext("SpringConfig.xml");
        DataSource ds = (DataSource) ctx.getBean("dataSourceDBDirect");
        //DataSource ds = (DataSource) ctx.getBean("dataSourcePool");
        DataJdbcDao taskDao = new DataJdbcDao();
        taskDao.setDataSource(ds);
        //给该会议增加一个人
        taskDao.insertMTUsr(iusrid, sMeetingid);

         //刷新显示列表
        Object mtId = 0;
        mtId = sMeetingid;
        try {
            CachedRowSet crs = this.getSessionBean1().getV_attendeeRowSet();
            crs.setObject(1, mtId);
            crs.execute();
        } catch (SQLException ex) {
            Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public String button2_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        Object oid = null;
        oid = this.getListbox2().getSelected();
        if (oid == null) {
            info("请选择要删除的已选择的人员");
            return null;
        }
        int iusrid = 0;
        String sMeetingid = "";
        iusrid = (Integer) oid;
        sMeetingid = this.getSessionBean1().getSUUID();
        //生成临时数据
        ApplicationContext ctx = new ClassPathXmlApplicationContext("SpringConfig.xml");
        DataSource ds = (DataSource) ctx.getBean("dataSourceDBDirect");
        //DataSource ds = (DataSource) ctx.getBean("dataSourcePool");
        DataJdbcDao taskDao = new DataJdbcDao();
        taskDao.setDataSource(ds);
        //给该会议增加一个人
        taskDao.removeMTUsr(iusrid, sMeetingid);

        //刷新显示列表
        Object mtId = 0;
        mtId = sMeetingid;
        try {
            CachedRowSet crs = this.getSessionBean1().getV_attendeeRowSet();
            crs.setObject(1, mtId);
            crs.execute();
        } catch (SQLException ex) {
            Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex);
        }


        return null;
    }

    public String button3_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        String app = this.getSessionBean1().getSApp();
        if (app.equalsIgnoreCase("room")) {
            return "toMain2";
        }else{
            return "manchoice2approvew2";
        }

    }

    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    public String button4_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        String sMeetingid = this.getSessionBean1().getSUUID();
        //生成临时数据
        ApplicationContext ctx = new ClassPathXmlApplicationContext("SpringConfig.xml");
        DataSource ds = (DataSource) ctx.getBean("dataSourceDBDirect");
        //DataSource ds = (DataSource) ctx.getBean("dataSourcePool");
        DataJdbcDao taskDao = new DataJdbcDao();
        taskDao.setDataSource(ds);
        int iusrid=0;
        //给该会议增加左侧列表中所有数据
        Iterator it = this.getListbox1().getListItems();
        ListItem itm;
        while (it.hasNext()){
            itm = (ListItem) it.next();
            iusrid=Integer.parseInt(itm.getValue());
            taskDao.insertMTUsr(iusrid, sMeetingid);
            //cls
            iusrid=0;
        }
        
        Object mtId = 0;
        mtId = sMeetingid;
        try {
            CachedRowSet crs = this.getSessionBean1().getV_attendeeRowSet();
            crs.setObject(1, mtId);
            crs.execute();
        } catch (SQLException ex) {
            Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public String button5_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        String sMeetingid = this.getSessionBean1().getSUUID();
        //生成临时数据
        ApplicationContext ctx = new ClassPathXmlApplicationContext("SpringConfig.xml");
        DataSource ds = (DataSource) ctx.getBean("dataSourceDBDirect");
        //DataSource ds = (DataSource) ctx.getBean("dataSourcePool");
        DataJdbcDao taskDao = new DataJdbcDao();
        taskDao.setDataSource(ds);
        //给该会议增加一个人
        taskDao.removeMTAllUsr(sMeetingid);
        Object mtId = 0;
        mtId = sMeetingid;
        try {
            CachedRowSet crs = this.getSessionBean1().getV_attendeeRowSet();
            crs.setObject(1, mtId);
            crs.execute();
        } catch (SQLException ex) {
            Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public void dropDown1_processValueChange(ValueChangeEvent event) {
        int grpid;
        grpid = (Integer) this.getDropDown1().getSelected();
        try {
            CachedRowSet crs = this.getSessionBean1().getV_usrListA();
            crs.setObject(1, grpid);
            crs.execute();
        } catch (SQLException ex) {
            Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

